<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>无标题文档</title>
    <link href="../css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../js/jquery.js"></script>
    <!--引入Vue组件 -->
    <script type="text/javascript" src="../js/vue.js"></script>
    <!--引入axios(vue的异步交互)组件 -->
    <script type="text/javascript" src="../js/axios.min.js"></script>
    <!-- 导入自定义的js配置文件-->
    <script type="text/javascript" src="../js/my_base_config.js"></script>
</head>

<body>
<div id="app">
    <div class="place">
        <span>位置：</span>
        <ul class="placeul">
            <li><a href="#">首页</a></li>
            <li><a href="#">表单</a></li>
            `
        </ul>
    </div>

    <div class="formbody">
        <div>
            <img v-if="admin.photo!=null&&admin.photo!=''" :src="`${server_url}${admin.photo}`"
                 style="width: 200px;"/>
            <img v-else src="../images/nopic.jpg" style="width: 200px;"/>
        </div>

        <div class="formtitle"><span>头像上传</span></div>
        <form>
            <ul class="forminfo">
                <li><input type="file" ref="fileSelect"/><i></i></li>
                <li><label>&nbsp;</label><input name="" type="button" class="btn" value="确认保存" @click="submit"/></li>
            </ul>
        </form>
    </div>

</div>
<script>
    new Vue({
        el: '#app',
        data: {
            server_url: server_url,
            admin: {}
        },
        methods: {
            readSession() {//读取当前登录账户信息
                axios
                    .get("/backstage/session")
                    .then(response => {
                        const responseData = response.data;
                        if (responseData.success) {
                            this.admin = responseData.data;
                        }
                    });
            },
            submit() {//提交表单
                const fileSelect = this.$refs.fileSelect;
                const file = fileSelect.files[0];
                const formDate = new FormData;
                formDate.append("file", file);
                axios
                    .post('/backstage/admin/photo',formDate)
                    .then(response => {
                        const resonseData = response.data;
                        if (resonseData.success) {//如果成功
                            this.readSession();
                        } else {//如果失败
                            alert(resonseData.msg)
                        }
                    })
            },
            mounted() {//当页面元素加载完成之后，自动执行里面的代码
                this.readSession();
            }
        }
    })


</script>
</body>
</html>
